package Verktoy;

import Beans.Klasse;
import DB.DBehandler;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 * Klasse som konverterer klasse mellom string og object
 * @author Gruppe1
 */
@ManagedBean(name="KlasseConverter")
public class KlasseConverter implements Converter{
    private ArrayList<Klasse> tabListe;
    private DBehandler db;
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        //SelectItems(myObject,myLabel) 
        String [] tab = value.trim().split(" ");
        db = new DBehandler();
        tabListe = db.hentAlleKlasser();
        for(Klasse k : tabListe){
            if(Integer.parseInt(tab[0])==k.getId())return k;
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Klasse k = (Klasse) value;
        return k.toString();
    }
}
